/*
 * Copyright 2017-8-30 13:56:26 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.ao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.page.PageUtils;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.ao.TemplateAO;
import com.lap.scp.admin.core.TemplateManager;
import com.lap.scp.admin.domain.TemplateDO;
import com.lap.scp.admin.query.TemplateQueryDO;

/**
 * <pre>
 * 模板应用对象
 * </pre>
 * 
 * @author 系统自动生成 Exp
 * @Date 2017-8-30 13:56:26
 * @since 1.0
 */
@Service("templateAO")
public class DefaultTemplateAO extends AbstractAO implements TemplateAO {

	@Resource
	private TemplateManager templateManager;

	@Override
	public Resp<Integer> deleteById(int templateId) {
		Resp<Integer> resp = new Resp<>();
		try {
			int flag = templateManager.deleteById(templateId);
			resp.setData(flag);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(templateId, e);
		}
		return resp;
	}

	@Override
	public Resp<TemplateDO> insertTemplate(TemplateDO templateDO, BindingResult error) {
		Resp<TemplateDO> resp = new Resp<>();
		try {
			super.validate(resp, error, templateDO);
			if (resp.isSuccess()) {
				templateManager.insertTemplate(templateDO);
				resp.setData(templateDO);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(templateDO, e);
		}
		return resp;
	}

	@Override
	public Resp<TemplateDO> queryById(int templateId) {
		Resp<TemplateDO> resp = new Resp<>();
		try {
			TemplateDO templateDO = templateManager.queryById(templateId);
			resp.setData(templateDO);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(templateId, e);
		}
		return resp;
	}

	@Override
	public PageResult<TemplateDO> queryPage(Page page, TemplateQueryDO templateQueryDO) {
		PageResult<TemplateDO> pr = new PageResult<TemplateDO>();
		try {
			List<TemplateDO> list = templateManager.queryPage(page, templateQueryDO);

			PageUtils.copyPage(pr, page);
			pr.setData(list);
		} catch (Exception e) {
			pr.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(new Object[] { page, templateQueryDO }, e);
		}
		return pr;
	}

	@Override
	public Resp<Integer> updateTemplate(TemplateDO templateDO, BindingResult error) {
		Resp<Integer> resp = new Resp<>();
		try {
			super.validate(resp, error, templateDO);
			if (resp.isSuccess()) {
				int flag = templateManager.updateTemplate(templateDO);
				resp.setData(flag);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(templateDO, e);
		}
		return resp;
	}

}
